package com.rxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rxt.common.result.Result;
import com.rxt.model.domain.KRemark;
import com.rxt.mapper.KRemarkMapper;
import com.rxt.model.vo.KRemarkVo;
import com.rxt.service.IKRemarkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxt.util.BaseContext;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Service
public class KRemarkServiceImpl extends ServiceImpl<KRemarkMapper, KRemark> implements IKRemarkService {

    /**
     * 根据knowledge - id 获取评论信息
     * @param id
     * @return
     */
    @Override
    public List<KRemark> getRemarkByKnowledgeID(Long id) {

        List<KRemark> list = this.list(new LambdaQueryWrapper<KRemark>().eq(KRemark::getKnowledgeId, id).orderByDesc(KRemark::getCreateTime));

        return list;

    }

    @Override
    public Result remark(KRemarkVo kRemarkVo) {

        KRemark remark = new KRemark();
        remark.setContent(kRemarkVo.getContent());
        remark.setKnowledgeId(kRemarkVo.getKnowledgeId());
        remark.setUserId(BaseContext.get().getId());

        this.save(remark);

        return Result.ok("评论成功");
    }

    @Override
    public Result deleteById(Long id) {

        boolean b = this.removeById(id);
        if (b){
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }
}
